﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMS.DAL;
using SMS.Business.Stocks;
using SMS.Business.Items;
using SMS.DAL.Providers;

namespace SMS.Items
{
    public partial class NewItem : System.Web.UI.Page
    {
        private StocksBusiness stockBusiness;
        private ItemBusiness newItemBusiness;

        string QryBarcode;
        string QryItemName;
        string QryItemMark;
        string QryStockID;
        string QrySellingPrice;
        string QryItemID;
        string QryType;

        private void GetQuaryStrings()
        {
            QryBarcode = Request.QueryString["Barcode"];
            QryItemName = Request.QueryString["ItemName"];
            QryItemMark = Request.QueryString["ItemMark"];
            QryStockID = Request.QueryString["Stock"];
            QryItemID = Request.QueryString["ItemID"];
            QryType = Request.QueryString["type"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");

            if (!IsPostBack)
            {
                GetQuaryStrings();

                SMS.DAL.User CurrentUser = (SMS.DAL.User)Session["CurrentUser"];
                stockBusiness = new StocksBusiness();
                StockUserSearchCriteria stockUserSearch = new StockUserSearchCriteria()
                {
                    UserID = CurrentUser.ID
                };

                if (QryStockID == null)
                {
                    ddlStock.DataSource = stockBusiness.GetByUserID(stockUserSearch);
                    ddlStock.DataValueField = "ID";
                    ddlStock.DataTextField = "Name";
                    ddlStock.DataBind();
                }
                else
                {
                    #region Bind Controls

                    ddlStock.SelectedValue = QryStockID;
                    ddlStock.Enabled = false;
                    if (QryItemID != null)
                    {
                        ItemBusiness itemBus = new ItemBusiness();
                        Item item = itemBus.GetByID(int.Parse(QryItemID));
                        txtBarCode.Text = item.BarCode;
                        txtBarCode.Enabled = false;
                        txtName.Text = item.Name;
                        txtName.Enabled = false;
                        txtMark.Text = item.Mark;
                        txtMark.Enabled = false;
                        txtManuCountry.Text = item.ManfCountry;
                        txtManuCountry.Enabled = false;
                        txtSellingPrice.Text = item.SellingPrice.ToString();
                        txtPartNumber.Text = item.PartNumber.ToString();
                        txtPartNumber.Enabled = false;
                    }
                    else
                    {
                        txtSellingPrice.Text = "0.0";
                        //txtSellingPrice.Enabled = false;
                        if (QryBarcode != string.Empty)
                        {
                            txtBarCode.Text = QryBarcode;
                            txtBarCode.Enabled = false;
                        }
                        if (QryItemName != string.Empty)
                        {
                            txtName.Text = QryItemName;
                            txtName.Enabled = false;
                        }
                        if (QryItemMark != string.Empty)
                        {
                            txtMark.Text = QryItemMark;
                            txtMark.Enabled = false;
                        }
                    }
                    #endregion
                }
            }
        }

        protected void btnAddToStock_Click(object sender, EventArgs e)
        {
            #region Fill Item
            Item newItem = new Item();
            newItem.BarCode = txtBarCode.Text;
            newItem.PartNumber = txtPartNumber.Text;
            newItem.Name = txtName.Text;
            newItem.SellingPrice = decimal.Parse(txtSellingPrice.Text);
            newItem.ManfCountry = txtManuCountry.Text;
            newItem.Mark = txtMark.Text;
            #endregion

            #region Fill StockItem
            StockItem newStockItem = new StockItem();

            GetQuaryStrings();

            if (QryStockID != null)
                newStockItem.StockID = int.Parse(QryStockID);
            else
                newStockItem.StockID = int.Parse(ddlStock.SelectedValue);

            newStockItem.MinCount = int.Parse(txtMinQuantity.Text);
            newStockItem.Count = int.Parse(txtQuantity.Text);

            #endregion

            newItemBusiness = new ItemBusiness();
            if (newItemBusiness.Insert(newItem, newStockItem))
            {
                cstmVld.IsValid = true;
            }
            else
                cstmVld.IsValid = false;

            //GetQuaryStrings();
            if(QryStockID != null)
            {
                int StockItemId = newStockItem.ID;
                if(QryType == "1")
                    Response.Redirect("~/Orders/InsertBuyingOrder.aspx?StockItemID=" + StockItemId);
                else
                    if(QryType =="2")
                        Response.Redirect("~/Orders/InsertSellingOrder.aspx?StockItemID=" + StockItemId);
            }


            this.txtQuantity.Text = string.Empty;
            this.txtMinQuantity.Text = string.Empty;
        }
    }
}